package com.sy.controller;

import com.sy.bean.Enterprise;
import com.sy.bean.Permissions;
import com.sy.bean.Renewal;
import com.sy.bean.RenewalAll;
import com.sy.bean.query.EnterpriseQuery;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.utils.BeanOfMap;
import com.sy.common.valid.ISave;
import com.sy.common.valid.IUpdate;
import com.sy.service.EnterpriseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "企业管理")
public class EnterpriseController extends BaseController {
    @Autowired
    EnterpriseService enterpriseService;

    @ApiOperation("保存企业")
    @PostMapping("/enterprise")
    public ResultBean<Enterprise> save(@Validated(ISave.class) @RequestBody Enterprise enterprise, BindingResult result) {
        validate(result);
        int rs = enterpriseService.save(enterprise);
        return gotoResult(rs,"保存");
    }

    @ApiOperation("企业列表")
    @PostMapping("/enterprise/list")
    public ResultBean<EnterpriseQuery> query(@RequestBody EnterpriseQuery enterpriseQuery) {
        PageBean pageBean = enterpriseService.query(BeanOfMap.bom(enterpriseQuery));
        return success("企业列表", pageBean);
    }

    @ApiOperation("修改企业")
    @PutMapping("/enterprise")
    public ResultBean<Enterprise> update(@RequestBody @Validated({IUpdate.class}) Enterprise enterprise, BindingResult result) {
        validate(result);
        int rs = enterpriseService.update(enterprise);
        return gotoResult(rs,"修改");
    }

    @ApiImplicitParam(name = "id", value = "企业id", required = true, dataTypeClass = Integer.class)
    @ApiOperation("删除企业")
    @DeleteMapping("/enterprise")
    public ResultBean delete(@RequestParam(name = "id") int id) {
        int rs = enterpriseService.delete(id);
        return gotoResult(rs,"删除");
    }

    @PutMapping("/enterprise/auth")
    @ApiOperation("权限")
    public ResultBean<Permissions> auth(@RequestBody Permissions permissions) {
        int rs = enterpriseService.auth(permissions);
        return gotoResult(rs,"修改");
    }
    @ApiOperation("续费")
    @PutMapping("/enterprise/renewal")
    public ResultBean renewal(@RequestBody @Validated Renewal renewal,BindingResult result){
        validate(result);
        int rs = enterpriseService.renewal(renewal);
        return rs==1?success("续费成功"):fail("续费失败");
    }
    @ApiOperation("企业资金信息")
    @PostMapping("/enterprise/fund")
    public ResultBean listOfFunds(@RequestBody Map map1){
        Map map = enterpriseService.queryMapper(map1);
        return success("资金信息",map);
    }
    @ApiOperation("导出企业")
    @PostMapping("/enterprise/exprot")
    public ResultBean exprot(@RequestBody Map map) {
        String url = enterpriseService.export(map);
        return success("导出企业", url);
    }
    @ApiOperation("续费列表")
    @PostMapping("/enterprise/ren")
    public ResultBean<RenewalAll> queryRen(@RequestBody Map map){
        RenewalAll bean = enterpriseService.queryRenById(map);
        return success("续费列表",bean);
    }
}
